package app.controllers.admin.vote;

import app.Const;
import app.models.DaoKit;
import app.models.sys.Settings;
import app.models.vote.Vote;
import app.models.vote.VoteCatetory;
import app.models.vote.VoteStat;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * .问卷调查
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-04-17 14:38
 * @since JDK 1.6
 */
public class VoteController extends BasicController {

    public static final String VOTE_FORM = "vote";

    public void index() {
        String vote_currency = Settings.vote();
        setAttr("currency", vote_currency);
        render("vote_index.ftl");
    }

    public void currency() {
        String currency = getPara("currency", "10");
        Settings.saveVote(currency);
        renderSuccess();
    }

    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        Page<Vote> pageRt = Vote.dao.datatable(criterias);

        renderDataTables(pageRt, criterias);
    }

    public void create() {
        Vote vote = new Vote();
        List<VoteCatetory> catetorys = VoteCatetory.dao.list();
        setAttr(VOTE_FORM, vote);
        setAttr(Const.ACTION_ATTR, Const.CREATE_ACTION);
        setAttr("catetorys", catetorys);
        render("vote_item.ftl");
    }

    public void edit() {
        Vote vote = Vote.dao.findById(getPara());
        List<VoteCatetory> catetorys = VoteCatetory.dao.list();
        List<String> optList = Const.BRACES_SPLITTER.splitToList(vote.getStr("opt"));
        setAttr("optList", optList);
        setAttr(VOTE_FORM, vote);
        setAttr("catetorys", catetorys);
        setAttr(Const.ACTION_ATTR, Const.EDIT_ACTION);
        render("vote_item.ftl");
    }

    public void remove() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_remove(SqlKit.sql("vote.remove"), ids)) {
            renderSuccess();
        } else {
            renderError("删除出现错误");
        }
    }

    public void save() {
        Vote vote = getModel(Vote.class, VOTE_FORM);
        List<String> values = Const.CRLF_SPLITTER.splitToList(vote.getStr("opt"));
        String opt = Const.BRACES_JOINER.join(values);

        if (DaoKit.isNew(vote)) {
            vote.set("state", true);
            vote.set("opt", opt);
            vote.save();

        } else {
            vote.set("opt", opt);
            vote.update();
            VoteStat.dao.deleteByVote(vote.getNumber(AppFunc.TABLE_PK_COLUMN).longValue());
        }
        for (int i = 0; i < values.size(); i++) {
            VoteStat stat = new VoteStat();
            stat.set("vote", vote.getNumber(AppFunc.TABLE_PK_COLUMN).longValue());
            stat.set("title", vote.getStr("title"));
            stat.set("choose_times", 0);
            stat.set("choose_item", i);
            stat.save();
        }
        redirect("/admin/vote/vote");
    }

    public void stop() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_update_by_id(SqlKit.sql("vote.stop"), ids)) {
            renderSuccess();
        } else {
            renderError("停用出现错误");
        }
    }

    public void run() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_update_by_id(SqlKit.sql("vote.run"), ids)) {
            renderSuccess();
        } else {
            renderError("启用出现错误");
        }
    }

    public void rsa() {
        int set = getParaToInt("set", 1);
        List<Vote> votes = Vote.dao.findBySet(set);
        setAttr("votes", votes);
    }


    public void stat() {
        int vote = getParaToInt(0, 0);
        if (vote > 0) {
            List<VoteStat> stats = VoteStat.dao.findbyVote(vote);
            renderJson(AjaxMessage.ok(stats));
        } else {
            renderJson(AjaxMessage.nodata());
        }
    }
}
